import React, { PureComponent } from 'react';
import { Table } from 'antd';
import { connect } from 'dva';
import styles from './TableList.less';

@connect()
export default class PateoTable extends PureComponent {
  // 分页、排序、筛选变化时触发
  onChange = (pagination, filters, sorter, extra) => {
    /**
     *  fetchParams 不使用分页fetchParams不必传
     *  extraArgs   一直生效的查询条件
     *  extraHandle  分页从 1 开始。传值为true. 默认从0开始
     */
    const {
      dispatch,
      fetchParams,
      formValues,
      extraHandle = false,
      currentName = 'current',
      fetchUrl,
      parameterUrl,
      pageSizeName = 'pageSize',
      dispatchFn,
    } = this.props;
    const current = extraHandle ? pagination.current : pagination.current - 1;

    // 提交没有成功修改
    const extraArgs = fetchParams?.extraArgs || {};
    const params = {
      [pageSizeName]: pagination.pageSize,
      [currentName]: current,
      ...formValues,
      ...filters,
      ...extraArgs,
    };
    const type = fetchParams?.type || fetchUrl;
    const url = fetchParams?.url || parameterUrl;
    if (dispatchFn) {
      dispatchFn(params);
    } else {
      dispatch({
        type,
        payload: { url, ...params },
      });
    }
  };

  render() {
    const { tableProps } = this.props;
    const columns = tableProps.columns;
    let tabWidth = 0;
    for (let i in columns) {
      if (columns[i].width) {
        tabWidth += columns[i].width;
      }
    }
    return (
      <Table
        onChange={this.onChange}
        scroll={{ x: tabWidth }}
        className={styles.my_table}
        size="small"
        {...tableProps}
      />
    );
  }
}
